//
//  HMStatuPictureView.swift
//  WymanWeibo
//
//  Created by wyman on 16/4/14.
//  Copyright © 2016年 wyman. All rights reserved.
//

import UIKit

import SDWebImage
private let HMStatusPictureViewCellMarin: CGFloat = 4

private let HMStatusPictureViewMaxColumn = 3

private let HMStatusPictureViewItemWH = ((UIScreen.mainScreen()).bounds.width - 2*HMStatusCellMargin - (CGFloat(HMStatusPictureViewMaxColumn)-1)*HMStatusPictureViewCellMarin )/CGFloat(HMStatusPictureViewMaxColumn)


class HMStatuPictureView: UICollectionView {

    
    private let ResuseIdentifier = "ResuseIdentifier"
    var pictureURLs: [NSURL]? {
        didSet{
    
        let size = calcViewSize()
            
        self.snp_updateConstraints { (make) in
            make.size.equalTo(size)
            }
//        HMPrint("配图大小:\(size)")
            
            self.reloadData()
        }
    }
  
    //计算配图的宽高
    private func calcViewSize() -> CGSize{
    
        layout.itemSize = CGSize(width: HMStatusPictureViewItemWH, height: HMStatusPictureViewItemWH)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        
        //判断图片的数量
        let count = pictureURLs?.count ?? 0
        
        if count == 0 {
//            HMPrint("没有图片")
            return CGSizeZero
    
        }
        
        if count == 1 {
            var size = CGSize(width: HMStatusPictureViewItemWH, height: HMStatusPictureViewItemWH)
            //返回图片大小
            let url = pictureURLs!.first!
            
            if let image = SDImageCache.sharedImageCache().imageFromDiskCacheForKey(url.absoluteString){
            
            size = image.size
//                HMPrint("真实图片大小为\(size)")

            size = CGSize(width: size.width*UIScreen.mainScreen().scale, height: size.height*UIScreen.mainScreen().scale)
            
            }
            if size.width<40 {
                size.width = 40
            }
            
            let screenWidth = UIScreen.mainScreen().bounds.width
            
            if size.width > screenWidth*0.5{
            size.width = screenWidth*0.5
            }
//            HMPrint("图片大小为\(size)")

            layout.itemSize = size
            return size
        }
        
        layout.minimumInteritemSpacing = HMStatusPictureViewCellMarin
        layout.minimumLineSpacing = HMStatusPictureViewCellMarin
        
        var colum = HMStatusPictureViewMaxColumn
        
        if count == 2 || count == 4 {
            colum = 2
        }
        
        let row = (count + colum - 1)/colum
        
        let width = CGFloat(colum)*HMStatusPictureViewItemWH + (CGFloat(colum)-1) * HMStatusPictureViewCellMarin
        
        let height = CGFloat(row) * HMStatusPictureViewItemWH + (CGFloat(row) - 1) * HMStatusPictureViewCellMarin
        return CGSize(width: width, height: height)
        
    }
    
    
    
    //设置一个flowlayout
    private var layout = UICollectionViewFlowLayout()
    
  
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //重写init方法
    init(){
    super.init(frame: CGRectZero, collectionViewLayout: layout)
    
        //设置数据源
        self.dataSource = self
        
        //
        self.registerClass(HMStatePictureCell.self, forCellWithReuseIdentifier: ResuseIdentifier)
    
    }
    
//    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//        super.touchesBegan(touches, withEvent: event)
////        HMPrint("哈哈哈哈哈=======")
//    }
    
}

//MARK - 类的扩展

extension HMStatuPictureView:UICollectionViewDataSource{
//实现数据源方法
    
    //

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureURLs?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
       let  cell = collectionView.dequeueReusableCellWithReuseIdentifier(ResuseIdentifier, forIndexPath: indexPath)as! HMStatePictureCell
       cell.imageURL =  pictureURLs![indexPath.row]
        return cell
    }
    

}



//MARK - 自定义cell
class HMStatePictureCell:UICollectionViewCell{

//    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//        super.touchesBegan(touches, withEvent: event)
//
//        HMPrint("哈哈哈哈")
//    }
    
    var imageURL: NSURL? {
        didSet{
        
        bgImage.sd_setImageWithURL(imageURL!)
        }
    
    }
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func prepareUI(){
    
        contentView.addSubview(bgImage)
        
        //添加约束
        bgImage.snp_updateConstraints { (make) in
            make.edges.equalTo(contentView)
        }
    
    
    }


    private lazy var bgImage:UIImageView = UIImageView()



}

